
#include "EEPROM.h"
#include "sensorData.h"
#include "allinit.h"
#include <Wire.h>
#include <BH1750FVI.h>
#include "MT.h"
uint32_t delayMS;
DHT_Unified dht(DHTPIN, DHTTYPE);
BH1750FVI myBH1750(BH1750_DEFAULT_I2CADDR, BH1750_CONTINUOUS_HIGH_RES_MODE_2, BH1750_SENSITIVITY_DEFAULT, BH1750_ACCURACY_DEFAULT);
int mt_flag = 1;       //mt是否自动
extern int open_flag;  //mt是否打开zhuangt
extern int close_flag; //mt是否关闭状态
void sensor_init()
{
  dht.begin();
  pinMode(dopin, INPUT);
  pinMode(RGB_R_PIN, OUTPUT);
  pinMode(RGB_G_PIN, OUTPUT);
  pinMode(RGB_B_PIN, OUTPUT);
  digitalWrite(RGB_B_PIN, HIGH);
  digitalWrite(RGB_G_PIN, HIGH);
  digitalWrite(RGB_R_PIN, HIGH);
  Serial.println(F("DHTxx Unified Sensor Example"));
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);

  Serial.println(F("------------------------------------"));
  Serial.println(F("Temperature Sensor"));
  Serial.print(F("Sensor Type: "));
  Serial.println(sensor.name);
  Serial.print(F("Driver Ver:  "));
  Serial.println(sensor.version);
  Serial.print(F("Unique ID:   "));
  Serial.println(sensor.sensor_id);
  Serial.print(F("Max Value:   "));
  Serial.print(sensor.max_value);
  Serial.println(F("C"));
  Serial.print(F("Min Value:   "));
  Serial.print(sensor.min_value);
  Serial.println(F("C"));
  Serial.print(F("Resolution:  "));
  Serial.print(sensor.resolution);
  Serial.println(F("C"));
  Serial.println(F("------------------------------------"));
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  Serial.println(F("Humidity Sensor"));
  Serial.print(F("Sensor Type: "));
  Serial.println(sensor.name);
  Serial.print(F("Driver Ver:  "));
  Serial.println(sensor.version);
  Serial.print(F("Unique ID:   "));
  Serial.println(sensor.sensor_id);
  Serial.print(F("Max Value:   "));
  Serial.print(sensor.max_value);
  Serial.println(F("%"));
  Serial.print(F("Min Value:   "));
  Serial.print(sensor.min_value);
  Serial.println(F("%"));
  Serial.print(F("Resolution:  "));
  Serial.print(sensor.resolution);
  Serial.println(F("%"));
  Serial.println(F("------------------------------------"));
  delayMS = sensor.min_delay / 1000;

  while (myBH1750.begin() != true) //SDA - D2, SCL - D1
  {
    Serial.println(F("ROHM BH1750FVI is not present")); //(F()) saves string to flash & keeps dynamic memory free
    delay(5000);
  }

  Serial.println(F("ROHM BH1750FVI is present"));
}

void sensor_display()
{
  double tem, hum;
  double temp;
  double data;
  int yudiaodata;
  int yudidodata;
  sensors_event_t event;

  dht.temperature().getEvent(&event);
  if (isnan(event.temperature))
  {
    Serial.println(F("Error reading temperature!"));
  }
  else
  {
    Serial.print(F("Temperature: "));
    tem = event.temperature;
    Serial.print(event.temperature);
    Serial.println(F("C"));
  }
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity))
  {
    Serial.println(F("Error reading humidity!"));
  }
  else
  {
    Serial.print(F("Humidity: "));
    hum = event.relative_humidity;
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
  }

  temp = (long)analogRead(33); // water
  data = (temp / 512) * 10;
  EEPROM.writeDouble(0x01, data);
  delay(10);
  Serial.print("the depth is:");
  Serial.print(data);
  Serial.println("mm");

  yudiaodata = 4095 - analogRead(aopin);
  Serial.println(yudiaodata);
  Serial.println("%");
  yudidodata = digitalRead(dopin);

  Serial.println(yudidodata);
  if (!yudidodata)
  {
    digitalWrite(RGB_R_PIN,LOW);
  }else digitalWrite(RGB_R_PIN,HIGH);
  
  if (mt_flag != 0)
  {
    if (!yudidodata && open_flag)
    {
      mt_play_open();
      open_flag = 0;
      close_flag = 1;
    }
    if (yudidodata && close_flag)
    {
      mt_play_close();
      close_flag = 0;
      open_flag = 1;
    }
  }

  Serial.println(F("DEMO 1: Sensitivity - 1.0, Continuous High Res II Mode"));
  Serial.print(F("Light level.........: "));
  Serial.print(myBH1750.readLightLevel());
  Serial.println(F(" +-0.5 lx"));
  Serial.print(F("Power for 555nm wave: "));
  Serial.print(myBH1750.readLightLevel() / 683);
  Serial.println(F(" Watt/m^2"));
  int light = myBH1750.readLightLevel();

  myBH1750.setResolution(BH1750_CONTINUOUS_HIGH_RES_MODE_2);
  delay(50);
  httppub(tem, hum, light, yudidodata, data);
  digitalWrite(LED_PIN, HIGH);
  delay(100);
  digitalWrite(LED_PIN, LOW);
}
